package com.honghu.villagepost.widget;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.honghu.villagepost.R;
import com.honghu.villagepost.adapter.RecLeftAdapter;
import com.honghu.villagepost.adapter.RecRightAdapter;
import com.honghu.villagepost.entity.Select;
import com.honghu.villagepost.adapter.RecLeftAdapter;
import com.honghu.villagepost.adapter.RecRightAdapter;
import com.lxj.xpopup.core.BottomPopupView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author: mqwang
 * @create: 2/20/21 11:36 AM
 * @Description:
 */
public class DateSelectPopup extends BottomPopupView {

    private DateCallBack callBack;

    public interface DateCallBack {
        void showValue(Select value);
    }

    public DateSelectPopup(@NonNull Context context, DateCallBack callBack) {
        super(context);
        this.callBack = callBack;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_date_select;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        RecyclerView recLeft = findViewById(R.id.rec_left);
        RecyclerView recRight = findViewById(R.id.rec_right);

        RecLeftAdapter leftAdapter = new RecLeftAdapter();
        recLeft.setAdapter(leftAdapter);

        RecRightAdapter rightAdapter = new RecRightAdapter();
        recRight.setAdapter(rightAdapter);

        SimpleDateFormat sf = new SimpleDateFormat("MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 1);

        List<Select> 明天ChildList = new ArrayList<>();

        Select 明天 = new Select(true, "明天");

        明天ChildList.add(new Select(false, sf.format(c.getTime()) ,"07:00" , "09:00"));
        明天ChildList.add(new Select(false, sf.format(c.getTime()) ,"09:00" , "11:00"));
        明天ChildList.add(new Select(false, sf.format(c.getTime()), "11:00" , "13:00"));
        明天ChildList.add(new Select(false, sf.format(c.getTime()), "13:00" ,"15:00"));
        明天ChildList.add(new Select(false, sf.format(c.getTime()) ,"15:00" ,"17:00"));
        明天ChildList.add(new Select(false, sf.format(c.getTime()), "17:00","19:00"));
        明天ChildList.add(new Select(false, sf.format(c.getTime()), "19:00" ,"21:00"));

        明天.setChildList(明天ChildList);
        leftAdapter.addData(明天);


        c.add(Calendar.DAY_OF_MONTH, 1);

        List<Select> 后天ChildList = new ArrayList<>();

        Select 后天 = new Select(false, "后天");

        后天ChildList.add(new Select(false, sf.format(c.getTime()) ,"07:00" , "09:00"));
        后天ChildList.add(new Select(false, sf.format(c.getTime()) ,"09:00" , "11:00"));
        后天ChildList.add(new Select(false, sf.format(c.getTime()), "11:00" , "13:00"));
        后天ChildList.add(new Select(false, sf.format(c.getTime()), "13:00" ,"15:00"));
        后天ChildList.add(new Select(false, sf.format(c.getTime()) ,"15:00" ,"17:00"));
        后天ChildList.add(new Select(false, sf.format(c.getTime()), "17:00","19:00"));
        后天ChildList.add(new Select(false, sf.format(c.getTime()), "19:00" ,"21:00"));

        后天.setChildList(后天ChildList);
        leftAdapter.addData(后天);

        leftAdapter.selectItem(0);
        rightAdapter.setNewData(明天.getChildList());

        leftAdapter.setOnItemClickListener((adapter, view, position) -> {
            Select selectDate = (Select) adapter.getData().get(position);
            leftAdapter.selectItem(position);
            rightAdapter.setNewData(selectDate.getChildList());

        });

        rightAdapter.setOnItemClickListener((adapter, view, position) -> {
            rightAdapter.selectItem(position);
            if (callBack != null){
                callBack.showValue(rightAdapter.getData().get(position));
                dismiss();
            }

        });
    }
}
